package cn.ekuma.epos.crm;

import java.util.Date;
import java.util.Properties;

import com.openbravo.bean.crm.CustomerPeople;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.shard.PeopleEditor;

import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class CustomerPeopleEditor  extends AbstractChildDAOJEditor<CustomerPeople,String>{
    private PeopleEditor peopleEditor;
    private JTextField txtDepartment;
    private JTextField txtPosition;
	private JTextArea txtMemo;
	public CustomerPeopleEditor(DirtyManager dirty) {
		super(dirty);
		setPreferredSize(new Dimension(450, 580));
		peopleEditor=new PeopleEditor(dirty);
		initComponents();
		txtDepartment.getDocument().addDocumentListener(dirty);
		txtPosition.getDocument().addDocumentListener(dirty);
		txtMemo.getDocument().addDocumentListener(dirty);
		
	}
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		add(peopleEditor, BorderLayout.NORTH);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel label = new JLabel(AppLocal.getIntString("label.position"));
		label.setBounds(241, 10, 81, 15);
		panel.add(label);
		
		JLabel label_1 = new JLabel(AppLocal.getIntString("label.department"));
		label_1.setBounds(10, 10, 95, 15);
		panel.add(label_1);
		
		txtDepartment = new JTextField();
		txtDepartment.setBounds(104, 7, 127, 21);
		panel.add(txtDepartment);
		txtDepartment.setColumns(10);
		
		txtPosition = new JTextField();
		txtPosition.setBounds(333, 7, 95, 21);
		panel.add(txtPosition);
		txtPosition.setColumns(10);
		
	    txtMemo = new JTextArea();
		txtMemo.setBorder(new TitledBorder(null, "memo", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		txtMemo.setBounds(10, 45, 418, 139);
		panel.add(txtMemo);
		
	}

	@Override
	public String getTabName() {
		return AppLocal.getIntString("label.CustomerPeople");
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public CustomerPeople createNew(Properties prop) {
		CustomerPeople newObj=new CustomerPeople();
		newObj.setCreateDate(new Date());
		if(parentNode!=null)
            newObj.setCustomerId(parentNode.getParentKey().getKey());
		return newObj;
	}

	@Override
	public void rebuild(CustomerPeople obj) throws BasicException {
		peopleEditor.rebuild(obj);
		
		obj.setDepartment(txtDepartment.getText());
		obj.setPosition(txtPosition.getText());
		obj.setMemo(txtMemo.getText());
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		
		txtDepartment.setText(obj.getDepartment());
		txtPosition.setText(obj.getPosition());
		txtMemo.setText(obj.getMemo());
		
		txtDepartment.setEditable(editAble);
		txtPosition.setEditable(editAble);
		txtMemo.setEditable(editAble);
	}

	@Override
	public void writeValueInsert(Properties prop) {
		super.writeValueInsert(prop);
		peopleEditor.writeValueInsert(prop);
	}
	@Override
	public void writeValueDelete(CustomerPeople value) {
		super.writeValueDelete(value);
		peopleEditor.writeValueDelete(value);
	}
	@Override
	public void writeValueEOF() {
		super.writeValueEOF();
		peopleEditor.writeValueEOF();
	}
	@Override
	public void writeValueEdit(CustomerPeople value) {
		super.writeValueEdit(value);
		peopleEditor.writeValueEdit(value);
	}
	
	@Override
	public Class getEditClass() {
		return CustomerPeople.class;
	}
}
